package com.Ajava1704.for并发包下的类.ForConditation;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 作者: LCG
 * 日期: 2020/6/22 23:33
 * 描述: 测试Conditation的使用
 */
public class TestConditation {

    private Lock lock=new ReentrantLock();

    private Condition conditionA=lock.newCondition();

    private Condition conditionB=lock.newCondition();

    /**
     * A
     */
    public void Await(){

        lock.lock();
        try{
            System.out.println("=======A await=====");

            conditionA.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    /**
     * Asingal
     */
    public void Asingal(){

        lock.lock();
        try{
            System.out.println("=======A Singal=====");
            conditionA.signalAll();
        }finally {
            lock.unlock();
        }

    }


    /**
     * A
     */
    public void Bwait(){

        lock.lock();
        try{
            System.out.println("=======B await=====");

            conditionB.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    /**
     * Asingal
     */
    public void Bsingal(){

        lock.lock();
        try{
            System.out.println("=======B Singal=====");
            conditionB.signalAll();
        }finally {
            lock.unlock();
        }

    }


}
